package com.hongtech.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hongtech.common.bean.AjaxResultVo;
import com.hongtech.common.page.PageData;
import com.hongtech.modules.common.BaseController;
import com.hongtech.modules.knowledge.dto.*;
import com.hongtech.modules.knowledge.entity.*;
import com.hongtech.modules.knowledge.service.*;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/app")
public class KnowledgeController extends BaseController {

    @Autowired
    private BannerService bannerService;

    @Autowired
    private BodyPartService bodyPartService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private DiseaseInfoService diseaseInfoService;

    @Autowired
    private DrugTypeService drugTypeService;

    @Autowired
    private DrugInfoService drugInfoService;

    @Autowired
    private FirstAidTypeService firstAidTypeService;

    @Autowired
    private FirstAidService firstAidService;

    @RequestMapping("/banner/list")
    @ApiOperation("横幅列表")
    public AjaxResultVo listBanner() {
        List<BannerDTO> bannerList = bannerService.listHomeBanner();
        return AjaxResultVo.success(bannerList);
    }

    @RequestMapping("/bodyPart/list")
    @ApiOperation("查询身体部位")
    public AjaxResultVo listBodyPart() {
        List<BodyPartDTO> bodyList = bodyPartService.listBodyPart();
        return AjaxResultVo.success(bodyList);
    }

    @RequestMapping("/department/list")
    @ApiOperation("查询科室")
    public AjaxResultVo listDepartment() {
        List<DepartmentDTO> departmentList = departmentService.listDepartment();
        return AjaxResultVo.success(departmentList);
    }

    @RequestMapping("/disease/page")
    @ApiOperation("分页查询疾病知识")
    public AjaxResultVo pageDiseaseInfo(Long bodyPartId, Long departmentId, Integer page, Integer size) {
        LambdaQueryWrapper<DiseaseInfoEntity> ew = new LambdaQueryWrapper<>();
        if (ObjectUtils.isNotEmpty(bodyPartId)) {
            ew.eq(DiseaseInfoEntity::getPositionId, bodyPartId);
        }
        if (ObjectUtils.isNotEmpty(departmentId)) {
            ew.eq(DiseaseInfoEntity::getDepartmentId, departmentId);
        }
        Map<String, Object> params = getPage(page, size);
        PageData<DiseaseInfoDTO> pages = diseaseInfoService.pageDiseaseInfo(params, ew);
        AjaxResultVo resultVo = AjaxResultVo.success();
        resultVo.put("total", pages.getTotal());
        resultVo.put("rows", pages.getList());
        return resultVo;
    }

    @RequestMapping("/disease/detail/{id}")
    @ApiOperation("疾病知识详情")
    public AjaxResultVo diseaseDetail(@PathVariable Long id) {
        DiseaseInfoDTO diseaseInfo = diseaseInfoService.get(id);
        return AjaxResultVo.success(diseaseInfo);
    }

    @RequestMapping("/drug/list")
    @ApiOperation("查询药品类别")
    public AjaxResultVo listDrugType() {
        List<DrugTypeDTO> drugList = drugTypeService.listDrugType();
        return AjaxResultVo.success(drugList);
    }

    @RequestMapping("/drug/page")
    @ApiOperation("分页查询药品知识")
    public AjaxResultVo pageDrugInfo(Long drugTypeId, Integer page, Integer size) {
        LambdaQueryWrapper<DrugInfoEntity> ew = new LambdaQueryWrapper<>();
        ew.eq(DrugInfoEntity::getTypeId, drugTypeId);
        Map<String, Object> params = getPage(page, size);
        PageData<DrugInfoDTO> pages = drugInfoService.pageDrugInfo(params, ew);
        AjaxResultVo resultVo = AjaxResultVo.success();
        resultVo.put("total", pages.getTotal());
        resultVo.put("rows", pages.getList());
        return resultVo;
    }

    @RequestMapping("/drug/detail/{id}")
    @ApiOperation("药品知识详情")
    public AjaxResultVo drugDetail(@PathVariable Long id) {
        DrugInfoDTO drugInfo = drugInfoService.get(id);
        return AjaxResultVo.success(drugInfo);
    }

    @RequestMapping("/firstAidType/list")
    @ApiOperation("查询急救类别")
    public AjaxResultVo listFirstAidType() {
        List<FirstAidTypeDTO> firstAidTypeList = firstAidTypeService.listFirstAidType();
        return AjaxResultVo.success(firstAidTypeList);
    }

    @RequestMapping("/firstAid/page")
    @ApiOperation("分页查询急救知识")
    public AjaxResultVo pageFirstAidInfo(Long firstTypeId, Integer page, Integer size) {
        LambdaQueryWrapper<FirstAidEntity> ew = new LambdaQueryWrapper<>();
        ew.eq(FirstAidEntity::getTypeId, firstTypeId);
        Map<String, Object> params = getPage(page, size);
        PageData<FirstAidDTO> pages = firstAidService.pageFirstAidInfo(params, ew);
        AjaxResultVo resultVo = AjaxResultVo.success();
        resultVo.put("total", pages.getTotal());
        resultVo.put("rows", pages.getList());
        return resultVo;
    }

    @RequestMapping("/firstAid/detail/{id}")
    @ApiOperation("急救知识详情")
    public AjaxResultVo firstAidDetail(@PathVariable Long id) {
        FirstAidDTO firstAid = firstAidService.get(id);
        return AjaxResultVo.success(firstAid);
    }

    @RequestMapping("/disease/home")
    @ApiOperation("分页查询常见疾病")
    public AjaxResultVo homePageDisease(Integer page, Integer size) {
        LambdaQueryWrapper<DiseaseInfoEntity> ew = new LambdaQueryWrapper<>();
        Map<String, Object> params = getPage(page, size);
        PageData<DiseaseInfoDTO> pages = diseaseInfoService.homePageDisease(params, ew);
        AjaxResultVo resultVo = AjaxResultVo.success();
        resultVo.put("total", pages.getTotal());
        resultVo.put("rows", pages.getList());
        return resultVo;
    }

}
